package com.example.user.service;
import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.alibaba.fastjson.JSON;
import com.example.pojo.GdUser;
import com.example.service.RpcLoginService;
import com.example.service.RpcTokenService;
import com.example.utils.Constants;
import com.example.utils.RedisUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
 * Created by Administrator on 2018/1/8.
 */
@Component
public class LoginServiceImpl implements LoginService {

    @DubboConsumer
    private RpcLoginService rpcLoginService;
    @DubboConsumer
    private RpcTokenService rpcTokenService;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public GdUser login(String userCode, String password) throws Exception {
        return rpcLoginService.login(userCode, password);
    }

    @Override
    public String generateToken(GdUser user) {
        return rpcTokenService.generateToken(user);
    }

    @Override
    public void save(String token, GdUser user) {
        redisUtils.set(token, JSON.toJSONString(user), Constants.Redis_Expire.SESSION_TIMEOUT);
    }

    @Override
    public void delete(String token) {
        if (redisUtils.exist(token))
        {
            redisUtils.delete(token);
        }
    }

    @Override
    public boolean validate(String token) {
        return redisUtils.validate(token);
    }
}
